package org.jeecg.modules.zdh.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.until.Sm4Util;
import org.jeecg.modules.zdh.entity.Fcfw;
import org.jeecg.modules.zdh.service.IFcfwService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;

import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 返程服务
 * @Author: jeecg-boot
 * @Date:   2023-12-25
 * @Version: V1.0
 */
@Slf4j
@Api(tags="返程服务")
@RestController
@RequestMapping("/zdh/fcfw")
public class FcfwController extends JeecgController<Fcfw, IFcfwService> {
	@Autowired
	private IFcfwService fcfwService;
	
	/**
	 * 分页列表查询
	 *
	 * @param fcfw
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "返程服务-分页列表查询")
	@ApiOperation(value="返程服务-分页列表查询", notes="返程服务-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Fcfw fcfw,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Fcfw> queryWrapper = QueryGenerator.initQueryWrapper(fcfw, req.getParameterMap());
		Page<Fcfw> page = new Page<Fcfw>(pageNo, pageSize);
		IPage<Fcfw> pageList = fcfwService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 * 添加
	 *
	 * @param fcfw
	 * @return
	 */
	@AutoLog(value = "返程服务-添加")
	@ApiOperation(value="返程服务-添加", notes="返程服务-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Fcfw fcfw) {
		fcfwService.save(fcfw);
		return Result.OK("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param fcfw
	 * @return
	 */
	@AutoLog(value = "返程服务-编辑")
	@ApiOperation(value="返程服务-编辑", notes="返程服务-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<?> edit(@RequestBody Fcfw fcfw) {
		fcfwService.updateById(fcfw);
		return Result.OK("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "返程服务-通过id删除")
	@ApiOperation(value="返程服务-通过id删除", notes="返程服务-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		fcfwService.removeById(id);
		return Result.OK("删除成功!");
	}

	 @DeleteMapping(value = "/delete2")
	 public Result<?> delete2() {
//		 fcfwService.removeById(id);
		 return Result.OK("删除成功!");
	 }
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "返程服务-批量删除")
	@ApiOperation(value="返程服务-批量删除", notes="返程服务-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.fcfwService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "返程服务-通过id查询")
	@ApiOperation(value="返程服务-通过id查询", notes="返程服务-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Fcfw fcfw = fcfwService.getById(id);
		return Result.OK(fcfw);
	}

	 @GetMapping(value = "/queryByPhone")
	 public Result<?> queryByPhone(@RequestParam(name="phone",required=true) String phone) {
		 QueryWrapper<Fcfw> queryWrapper = new QueryWrapper<Fcfw>();
		 queryWrapper.eq("手机号",phone);
		 Fcfw fcfw = fcfwService.getOne(queryWrapper,false);
		 return Result.OK(fcfw);
	 }

  /**
   * 导出excel
   *
   * @param request
   * @param fcfw
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, Fcfw fcfw) {
//      return super.exportXls(request, fcfw, Fcfw.class, "返程服务");

	  List<Fcfw> exportList = fcfwService.list();
	  for (Fcfw item : exportList) {
		  item.set姓名(Sm4Util.SM4decrypt(item.get姓名()));
		  item.set手机号(Sm4Util.SM4decrypt(item.get手机号()));
		  item.set是否接车(Sm4Util.SM4decrypt(item.get是否接车()));
		  item.set接车日期(Sm4Util.SM4decrypt(item.get接车日期()));
		  item.set接车车次(Sm4Util.SM4decrypt(item.get接车车次()));
		  item.set接车地点(Sm4Util.SM4decrypt(item.get接车地点()));
		  item.set接车人员(Sm4Util.SM4decrypt(item.get接车人员()));
		  item.set返程日期(Sm4Util.SM4decrypt(item.get返程日期()));
		  item.set出发地点(Sm4Util.SM4decrypt(item.get出发地点()));
		  item.set到达地点(Sm4Util.SM4decrypt(item.get到达地点()));
		  item.set返程车次(Sm4Util.SM4decrypt(item.get返程车次()));
		  item.set返程票号(Sm4Util.SM4decrypt(item.get返程票号()));
		  item.set是否送车(Sm4Util.SM4decrypt(item.get是否送车()));
	  }
	  // Step.3 AutoPoi 导出Excel
	  ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
	  //此处设置的filename无效 ,前端会重更新设置一下
	  mv.addObject(NormalExcelConstants.FILE_NAME, "返程服务");
	  mv.addObject(NormalExcelConstants.CLASS, Fcfw.class);
	  //update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
	  ExportParams exportParams=new ExportParams();
//	  exportParams.setImageBasePath(jeecgBaseConfig.getPath().getUpload());
	  //update-end--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置----------------------
	  mv.addObject(NormalExcelConstants.PARAMS,exportParams);
	  mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
	  return mv;
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, Fcfw.class);
  }

}
